//
//  LoginViewController.m
//  habit
//
//  Created by huck on 15-9-9.
//  Copyright (c) 2015年 custom. All rights reserved.
//
#import "LoginViewController.h"
#import "HttpRequest.h"
#import "AppUtils.h"
#import "ServerMng.h"
#import "LoginDataMng.h"
#import "Config.h"


@interface LoginViewController ()
{
    
}
@end

@implementation LoginViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubviews];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)initSubviews
{
    self.title = @"用户登录";
    _usernameView.layer.borderWidth = 1;
    _usernameView.layer.borderColor  = [UIColor colorWithString:COLOR_EDEEEF].CGColor;
    _passwardView.layer.borderWidth = 1;
    _passwardView.layer.borderColor  = [UIColor colorWithString:COLOR_EDEEEF].CGColor;
//    _usernameTextField.text = @"admin";
//    _usernameTextField.text = @"test";//督导员
//    _usernameTextField.text = @"zzz"; //五水办
//    _usernameTextField.text = @"zyn"; //整改员
//    _usernameTextField.text = @"shijd";//责任人
//        _usernameTextField.text = @"dxjddd";
//    _passwordTextField.text = @"123456";
    if([AppUtils isNotEmpty:LOGIN_USER.userName]){
        _usernameTextField.text = LOGIN_USER.userName;
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self hideInput];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([_usernameTextField isFirstResponder]) {
        [_passwordTextField becomeFirstResponder];
    }
    if (textField.returnKeyType == UIReturnKeyDone) {
        [self loginBtnClick :nil];
    }
    return YES;
}

- (IBAction)loginBtnClick:(UIButton *)sender {
    
    NSString *username = _usernameTextField.text;
    NSString *password = _passwordTextField.text;
    [self hideInput];

    if ([@"" isEqualToString:username]) {
        [_usernameTextField becomeFirstResponder];
    } else if ([@"" isEqualToString:password]) {
        [_passwordTextField becomeFirstResponder];
    } else {
        if([AppUtils hasConnected:YES]){
//            LOGIN_USER.userName = username;
//            LOGIN_USER.password = password;
            [self requestLoginAPP:username passward:password];
        }
    }
}


-(void)requestLoginAPP:(NSString *)username passward:(NSString *)pwd
{
    [Toast loading];
    [[LoginDataMng sharedInstance] login:username password:pwd isReload:NO complete:^(BOOL success) {
        [Toast loaded];
        if(success){
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}


@end
